package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;

import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class UploadItem extends BindingListItem {
    public static final int TYPE_VIDEO = 1;
    public static final int TYPE_ARTICLE = 0;
    //    标题title、浏览数browser、收藏数collectcoun、上传日期systime
    private int type;
    private int id;
    private String title;
    private int browser;
    private int collectcoun;
    private Date systime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Bindable
    public int getBrowser() {
        return browser;
    }

    public void setBrowser(int browser) {
        this.browser = browser;
    }

    @Bindable
    public int getCollectcoun() {
        return collectcoun;
    }

    public void setCollectcoun(int collectcoun) {
        this.collectcoun = collectcoun;
    }

    @Bindable
    public Date getSystime() {
        return systime;
    }

    public void setSystime(Date systime) {
        this.systime = systime;
    }

    public UploadItem(Context context, int layoutId){
        super(context, layoutId);
    }

    public void onClickItem(View view){
        if(type == TYPE_ARTICLE){
            ArticleItem item = new ArticleItem(getContext(),0);
            item.setArticleId(id);
            item.setTitle(title);
            item.setBrowser(browser);
            ArticleDetail detail = new ArticleDetail(getContext(), item);
            CommonFragment fragment = new CommonFragment();
            fragment.setDataModel(detail);
            fragment.setLayoutId(R.layout.f_article_detail);
            fragment.setVariableId(BR.detail);
            HomeActivity.switchFragment((Activity)getContext(), fragment);
        }
    }

    public void onClickDelete(View view){
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(type == TYPE_ARTICLE) {
                    if (WebApi.deleteDocument(getContext(), id)) {
                        HomeActivity.notifyListChange(ListBindingAdapter.ACTION_DEL, UploadItem.this);
                    }
                }
            }
        }).start();
    }
}
